/** \file uart.h
 **
 ** uart file
 **
  ** History:
  **   - 2013-11-22  1.0  Mark  First Version
  *************************************************************************/
#ifndef _I2C_H_
#define _I2C_H_
/*****************************************************************************/
/* Include files                                                             */
/*****************************************************************************/
#include "mcu.h"
#include "base_types_l3.h"
/*****************************************************************************/
/* Global pre-processor symbols/macros ('#define')                           */
/*****************************************************************************/
#ifndef CLOCK_FREQ
    #ifdef __HCLK
        #define CLOCK_FREQ __HCLK>>APBC0_PSR_Val
    #else
        #define CLOCK_FREQ 40000000UL
    #endif
#endif

#define I2C_BAUDRATE(x)  (uint16_t)((CLOCK_FREQ+x/2)/(uint32_t)x - 1)


#define TIME_OUT          500       // error counter time-out
#define TIME_OUT_INT      3000      // Time out for INT flag polling

#define I2C_READ       	  1         // read from slave
#define I2C_WRITE      	  0         // write to slave



/*****************************************************************************/
/* Global type definitions ('typedef')                                       */
/*****************************************************************************/

/*****************************************************************************/
/* Global variable declarations ('extern', definition in C source)           */
/*****************************************************************************/

/*****************************************************************************/
/* Global function prototypes ('extern', definition in C source)             */
/*****************************************************************************/
boolean_t I2c_Master_Init(uint8_t u8I2c, uint32_t Baudrate);
int32_t I2cStart(uint8_t, uint8_t);
int32_t I2cSendByte(uint8_t, uint8_t);
int32_t I2cContinueSendByte(uint8_t, uint8_t);
uint16_t I2cWriteStop(uint8_t);
uint16_t I2cReadStop(uint8_t);

int8_t I2cReceiveByte(uint8_t);
int8_t I2cReceiveByteSeq(uint8_t, uint8_t);

int8_t I2c_readByte(uint8_t ch, uint8_t devAddr, uint8_t regAddr, uint8_t *u8Data);
boolean_t I2c_writeByte(uint8_t ch, uint8_t devAddr, uint8_t regAddr, uint8_t data);
boolean_t I2c_writeBytes(uint8_t ch, uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t* data);
int8_t I2c_readBytes(uint8_t ch, uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data);
boolean_t I2c_writeBits(uint8_t ch, uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data);
boolean_t I2c_writeBit(uint8_t ch, uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data);

void I2cRxIrqHandler(uint8_t mfsn);
#endif
